﻿using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Poco;
using Shell.DomainDot;

namespace Framework.Domain
{
    /// <summary>
    /// 经销商分组的上下文
    /// </summary>
    public class AgencyGroupContext : DomainDotBase<AgencyGroup>
    {
        /// <summary>
        /// 所有经销商
        /// </summary>
        public WithMany<Agency> Agencies { private get; set; }

        /// <summary>
        /// 所有经销商分组
        /// </summary>
        public WithAll<AgencyGroup> AgencyGroups { private get; set; }

        /// <summary>
        /// 获取分组下的所有经销商
        /// </summary>
        public IEnumerable<Agency> GetAllAgency()
        {
            return this.Agencies;
        }

        /// <summary>
        /// 获得所有经销商分组
        /// </summary>
        public IEnumerable<AgencyGroup> GetAllAgencyGroup()
        {
            return this.AgencyGroups;
        }

        /// <summary>
        /// 根据一个Guid查找出一个经销商分组
        /// </summary>
        public AgencyGroup Find(Guid identity)
        {
            var agencyGroup = this.AgencyGroups.SingleOrDefault(a => a.Id == identity);
            if (agencyGroup != null)
            {
                //TODO  这里应该查找一下是否有和分组关联的经销商
                return agencyGroup;
            }
            return null;
        }

        /// <summary>
        /// 删除一个经销商分组
        /// </summary>
        public void Delete(AgencyGroup instance)
        {
            if (instance != null)
            {
                this.AgencyGroups.Delete(instance.Id);
                this.AgencyGroups.Submit();
            }
        }

        /// <summary>
        /// 修改一个经销商分组
        /// </summary>
        public void Edit(AgencyGroup instance)
        {
            var agencyGroup = this.AgencyGroups.SingleOrDefault(ag => ag.Id == instance.Id);
            if (agencyGroup != null)
            {
                agencyGroup.Name = instance.Name;
                this.AgencyGroups.Submit();
            }
        }
    }
}
